//
//  FWViewController.m
//  PhotoBrowser
//
//  Created by acct<blob>=0xE4BAA6616E64E5BF83  447246and457703 on 01/05/2018.
//  Copyright (c) 2018 acct<blob>=0xE4BAA6616E64E5BF83  447246and457703. All rights reserved.
//

#import "FWViewController.h"
#import "FWCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "SHPhotoBrowser.h"

@interface FWViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, PhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end

@implementation FWViewController
static NSString *const ID = @"FWCollectionViewCell";

- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
    
}

- (void)viewDidLoad{
    [super viewDidLoad];
	
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([FWCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
    
    NSString *ur1 = @"http://f.hiphotos.baidu.com/image/pic/item/4ec2d5628535e5dd922f99387cc6a7efcf1b62d1.jpg";
    NSString *ur2 = @"http://h.hiphotos.baidu.com/image/pic/item/f7246b600c33874454bf9b965b0fd9f9d62aa0e6.jpg";
    NSString *ur3 = @"http://f.hiphotos.baidu.com/image/pic/item/0bd162d9f2d3572c64cf016d8013632762d0c356.jpg";
    NSString *ur4 = @"http://b.hiphotos.baidu.com/image/pic/item/a71ea8d3fd1f41344e9de1982f1f95cad0c85eaa.jpg";
    NSString *ur5 = @"http://h.hiphotos.baidu.com/image/pic/item/30adcbef76094b364304532daacc7cd98c109d58.jpg";
    NSString *ur6 = @"http://h.hiphotos.baidu.com/image/pic/item/aa18972bd40735fae9d3b17897510fb30e2408cb.jpg";
    NSString *ur7 = @"http://e.hiphotos.baidu.com/image/pic/item/77c6a7efce1b9d16c9c5fea0fadeb48f8d5464f4.jpg";
    NSString *ur8 = @"http://g.hiphotos.baidu.com/image/pic/item/a5c27d1ed21b0ef4e5f9618fd7c451da80cb3eca.jpg";
    [self.dataSource addObject:ur1];
    [self.dataSource addObject:ur2];
    [self.dataSource addObject:ur3];
    [self.dataSource addObject:ur4];
    [self.dataSource addObject:ur5];
    [self.dataSource addObject:ur6];
    [self.dataSource addObject:ur7];
    [self.dataSource addObject:ur8];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FWCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    NSURL *url = [NSURL URLWithString:_dataSource[indexPath.row]];
    [cell.imageView sd_setImageWithURL:url];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [SHPhotoBrowser show:self delegate:self index:indexPath.row configBlock:nil];
}

- (NSInteger)numberOfPhotos:(SHPhotoBrowser *)photoBrowser{
    return _dataSource.count;
}

- (UIImage *)photoBrowser:(SHPhotoBrowser *)photoBrowser index:(NSInteger)index{
    FWCollectionViewCell *cell = [self collectionView:_collectionView cellForItemAtIndexPath:[NSIndexPath  indexPathForRow:index inSection:0]];
    return cell.imageView.image;
}

- (UIView *)photoBrowser:(SHPhotoBrowser *)photoBrowser showViewIndex:(NSInteger)index{
    FWCollectionViewCell *cell = [self collectionView:_collectionView cellForItemAtIndexPath:[NSIndexPath  indexPathForRow:index inSection:0]];
    return cell;
}
- (NSURL *)photoBrowser:(SHPhotoBrowser *)photoBrowser originalQualityUrlIndex:(NSInteger)index{
    NSURL *url = [NSURL URLWithString:_dataSource[index]];
    return url;
}
@end
